package com.renli.service.impl;

import com.renli.bean.ResultJson;
import com.renli.mapper.PostMapper;
import com.renli.pojo.Post;
import com.renli.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Override
    public ResultJson add(Post post) {
        if (postMapper.add(post) == 1) {
            return new ResultJson().setSuccess("添加成功");
        } else {
            return new ResultJson().setSuccess("添加失败");
        }
    }

    @Override
    public ResultJson<List<Map<String,Object>>> findAll(Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        /*
        * "id:明细id" +
            "currentPage：当前页（默认1）" +
            "pageSize：页面大小（默认6）" +
            "name：职位名称" +
            "depId：所属部门" +
            "state：是否启用（0未启用）*/
        if (map.get("currentPage") == null || map.get("currentPage").equals("")) {
            map.put("currentPage", 1);
        }
        if (map.get("pageSize") == null || map.get("pageSize").equals("")) {
            map.put("pageSize", 6);
        }


        Integer start = (Integer) map.get("pageSize") * ((Integer) map.get("currentPage") - 1);
        map.put("start", start);

        ResultJson<List<Map<String,Object>>> resultJson = new ResultJson<>();
        //总数
        Long total = postMapper.findAllCount(map);
        //数据集合
//        List<Post> list = postMapper.findAll(map);
        List<Map<String,Object>> list = postMapper.findAll(map);

        resultJson.setSuccess("查询成功", list,total);
        return resultJson;
    }

    @Override
    public ResultJson del(Integer postId) {
        ResultJson<List<Post>> resultJson = new ResultJson<>();
        if (postMapper.del(postId) == 1) {
            resultJson.setSuccess("删除成功");
        } else {
            resultJson.setSuccess("删除失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson update(Post post) {
        ResultJson resultJson = new ResultJson();

        if (postMapper.update(post) == 1) {
            resultJson.setSuccess("修改成功");
        } else {
            resultJson.setSuccess("修改失败");
        }
        return resultJson;
    }
}
